package com.example.fengqiqi

import android.content.Context
import java.io.File

class FileHelper(private val context: Context) {

    private val fileName = "students.txt"
    init {
        // 创建文件，如果文件不存在
        val file = File(context.filesDir, fileName)
        if (!file.exists()) {
            file.createNewFile()
        }
    }

    fun saveStudents(students: List<Student>) {
        val file = File(context.filesDir, fileName)
        file.writeText(students.joinToString("\n") { "${it.id},${it.name},${it.gender},${it.className}" })
    }

    fun loadStudents(): List<Student> {
        val file = File(context.filesDir, fileName)
        if (!file.exists()) return emptyList()
        return file.readLines().map {
            val parts = it.split(",")
            Student(parts[0], parts[1], parts[2], parts[3])
        }
    }
}